package android.medicalcloud.com.yiapp.yilian.ui.ac;

import android.app.Dialog;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.logic.CommonController;
import android.medicalcloud.com.yiapp.yilian.logic.UserController;
import android.medicalcloud.com.yiapp.yilian.ui.view.SquaredImageView;
import android.medicalcloud.com.yiapp.yilian.ui.view.dialog.RunProgressDialog;
import android.medicalcloud.com.yiapp.yilian.utils.android.DialogFacory;
import android.medicalcloud.com.yiapp.yilian.utils.android.ImageLoader;
import android.medicalcloud.com.yiapp.yilian.utils.android.ToastTool;
import android.view.View;
import android.widget.TextView;



import org.json.JSONObject;

import java.io.Serializable;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by User on 2016/12/8.
 */
public class SubmitAvatarActivity extends BaseActivity implements View.OnClickListener, ViewNetCallBack {
    SquaredImageView iamge;
    TextView cancel;
    TextView complete;
    String path;
    RunProgressDialog dialog;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        dialog = DialogFacory.getInstance().createProgressRunDialog(this, "正在上传");
        iamge = (SquaredImageView)findViewById(R.id.iamge);
        cancel = (TextView)findViewById(R.id.cancel);
        complete = (TextView)findViewById(R.id.complete);
    }

    @Override
    protected void initEvent() {
        complete.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        path = getIntent().getStringExtra("url");
        ImageLoader.getInstance(this, R.drawable.default_image).displayLocalImage(path, iamge);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_submit_avatar;
    }

    @Override
    public void onConnectStart(Object o) {
        if (!dialog.getCustomProgressDialog().isShowing())
            dialog.show();
    }

    @Override
    public void onConnectEnd() {
            if (dialog.getCustomProgressDialog().isShowing())
                dialog.dismiss();
        }


    @Override
    public void onFail(Exception e, Object o, String reslut) {
        ToastTool.show("上传失败");
//        if (((HttpConfig) o).getType() == HttpConfig.updateAvatar.getType()) {
//            if (dialog.getCustomProgressDialog().isShowing())
//                dialog.dismiss();
//        }
    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
        if (flag == HttpConfig.uploadImage.getType()) {
            finish();
        }
//        if (flag == HttpConfig.updateAvatar.getType()) {
//            if (dialog.isShowing())
//                dialog.dismiss();
//            BaseModel model = (BaseModel) result;
//            if (model.getCode() == 0) {
//                ToastTool.show("上传成功");
//                finish();
//            } else {
//                ToastTool.show("上传失败");
//            }
//        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete:
                CommonController.getInstance().upLoadImage(SubmitAvatarActivity.this, path);
                break;
            case R.id.cancel:
                finish();
                break;
        }
    }
}
